package com.elvin.platform.service.impl;

import com.elvin.platform.beans.Organize;
import com.elvin.platform.beans.PageVo;
import com.elvin.platform.beans.UserOrganization;
import com.elvin.platform.mapper.OrganizeMapper;
import com.elvin.platform.mapper.UserOrganizationMapper;
import com.elvin.platform.service.UserOrganizationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.elvin.platform.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhenchl
 * @since 2022-05-27
 */
@Service
public class UserOrganizationServiceImpl extends ServiceImpl<UserOrganizationMapper, UserOrganization> implements UserOrganizationService {

    @Resource
    private  UserOrganizationMapper  mapper;
    @Resource
    private OrganizeMapper organizeMapper;

  @Override
    public int selectPageCount(UserOrganization entity) {
        return mapper.selectPageCount(entity);
    }


  @Override
    public List<UserOrganization> selectForPage(UserOrganization entity) {
        return mapper.selectForPage(entity);
    }


  /**
     * 根据用户id获取用户所属组织和下级组织
     * @param map
     * @return
     */
    @Override
    public List<Organize> getOrganizationsByUserId(Map<String, Object> map) {
        //1、先根据用户获取用户关联的组织信息
        //2、根据组织id查找到组织本身及子组织机构的组织信息
        //3、组装数据
        UserOrganization userOrganization = new UserOrganization();
        userOrganization.setUserId((Long) map.get("userId"));
        PageVo pageVo = new PageVo();
        pageVo.setPage(1);
        pageVo.setPageSize(100000000);
        userOrganization.setPageVo(pageVo);
        List<UserOrganization> list = mapper.selectForPage(userOrganization);
        List<Organize> organizeList  = new ArrayList<>();
        for (UserOrganization organization : list) {
            HashMap<String,Object> param = new HashMap<>();
            String ids = organizeMapper.selectSelfAndChildId(organization.getOrganizeId());
            param.put("ids",ids);
            List<Organize> organizes = organizeMapper.selectSelfAndChild(param);
            organizeList.addAll(organizes);
        }
        return organizeList;
    }


  @Override
  public Result<Object> addAndCheck(UserOrganization entity) {
    Result<Object> result = new Result<>();
    //1、先根据组织id获取上下级组织信息
    //2、如果组织人员表中没有上下级组织信息，插入，有，则返会错误信息
    HashMap<String,Object> param = new HashMap<>();
    String childIds = organizeMapper.selectSelfAndChildId(entity.getOrganizeId());
    String parentIds = organizeMapper.selectSelfAndParentId(entity.getOrganizeId());
    StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(childIds);
    if (parentIds.length()>0){
      if (childIds.length()>0){
        stringBuffer.append(",");
      }
      stringBuffer.append(parentIds);
    }
    String ids = stringBuffer.toString();
    param.put("ids",ids);
    param.put("userId",entity.getUserId());
    List<UserOrganization> userOrganizationList = mapper.selectListByIds(param);
    if (userOrganizationList.size()>0){
      result.fail(null,"该人员已经分配组织信息，不能重复分配！");
    }else{
      mapper.insert(entity);
      result.success("","新增成功！");
    }
    return result;
  }


}
